//
//  SearchOidViewController.m
//  EducationOID
//
//  Created by slz on 2017/10/30.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "SearchOidViewController.h"
#import "BaseHttpTool.h"

@interface SearchResultSuccessView : UIView
@property (nonatomic,strong) UILabel *nameLabel;
@property (nonatomic,strong) UILabel *OIDLabel;
@property (nonatomic,strong) UILabel *typeLabel;

@end

@implementation SearchResultSuccessView
- (instancetype)init
{
    self = [super init];
    if (self){
        UIColor * textColor = [UIColor whiteColor];
        
        _nameLabel = [UILabel new];
        _nameLabel.textColor =textColor;
        _nameLabel.fontSize = 16;
        [self addSubview:_nameLabel];
        [_nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.mas_left).offset(18);
            make.right.mas_equalTo(self.mas_right).offset(-18);
            make.top.mas_equalTo(self.mas_top).offset(10);
        }];
        
        _OIDLabel = [UILabel new];
        _OIDLabel.textColor = textColor;
        _OIDLabel.fontSize = 16;
        [self addSubview:_OIDLabel];
        [_OIDLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_nameLabel.mas_left);
            make.right.mas_equalTo(_nameLabel.mas_right);
            make.top.mas_equalTo(_nameLabel.mas_bottom).offset(20);
        }];
        
        _typeLabel = [UILabel new];
        _typeLabel.textColor = textColor;
        _typeLabel.fontSize = 16;
        [self addSubview:_typeLabel];
        [_typeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(_nameLabel.mas_left);
            make.right.mas_equalTo(_nameLabel.mas_right);
            make.top.mas_equalTo(_OIDLabel.mas_bottom).offset(20);
        }];
        
    }
    return self;
}

- (void)setWithJson:(MFWJson *)subJson
{
    _nameLabel.text = [NSString stringWithFormat:@"名称：%@",[subJson stringValueForKey:@"name"]];
    _OIDLabel.text = [NSString stringWithFormat:@"OID：%@",[subJson stringValueForKey:@"url"]];
    NSInteger type = [subJson intValueForKey:@"oid_type"];
    NSString * typeString = nil;
    switch (type) {
        case 1:
           typeString  = @"网站";
            break;
            case 2:
            typeString  = @"微信公众号";

            break;
            case 3:
          typeString  = @"OID分中心";

            break;
            case 4:
            typeString  = @"OID应用";
            break;
            
        default:
            break;
    }
    _typeLabel.text = [NSString stringWithFormat:@"类型：%@",typeString];
}
@end

@interface SearchOidViewController()<UITextFieldDelegate>
@property (nonatomic,strong) UITextField *searchTextField;
@property (nonatomic,strong) UIButton *searchButton;
@property (nonatomic,strong) UILabel *errorLabel;
@property (nonatomic,strong) SearchResultSuccessView *resultView;
@end

@implementation SearchOidViewController

- (void)viewDidLoad {
    self.showNavigationBar = YES;
    [self setControllerTitle:@"查询OID"];
    self.view.backgroundColor = kBack_Color_Default;
    UIBarButtonItem * item = [UIBarButtonItem itemWithImage:@"top_back_1" selectImage:nil addTarget:self andAction:@selector(backAction)];
    self.navigationItem.leftBarButtonItem = item;
    
    UIImageView * leftImageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.contentMode = UIViewContentModeCenter;
    leftImageView.image = [UIImage imageNamed:@"search_icon_1"];
    
    
    _searchTextField = [[UITextField alloc]init];
    _searchTextField.placeholder = @"请输入OID";
    _searchTextField.leftViewMode = UITextFieldViewModeAlways;
    _searchTextField.leftView = leftImageView;
    _searchTextField.delegate = self;
    _searchTextField.backgroundColor = [UIColor whiteColor];
    _searchTextField.layer.cornerRadius = 3;
    _searchTextField.layer.masksToBounds = YES;
    [self.view addSubview:_searchTextField];
    [_searchTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.mas_equalTo(16);
        make.trailing.mas_equalTo(-74);
        make.height.mas_equalTo(30);
        make.top.mas_equalTo(40);
    }];
    
    _searchButton = [UIButton buttonWithType:UIButtonTypeCustom];
    _searchButton.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:_searchButton];
    [_searchButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(_searchTextField.mas_right).offset(8);
        make.size.mas_equalTo(CGSizeMake(50, 30));
        make.top.mas_equalTo(_searchTextField.mas_top);
    }];
    [_searchButton addTarget:self action:@selector(searchAction) forControlEvents:UIControlEventTouchUpInside];
    [_searchButton setTitle:@"查询" forState:UIControlStateNormal];
    _searchButton.titleLabel.fontSize = 15;
    [_searchButton setTitleColor:kDark_TextColor_Default forState:UIControlStateNormal];
    _searchButton.layer.cornerRadius = 3;
    _searchButton.layer.masksToBounds = YES;
    
    _resultView =[SearchResultSuccessView new];
    [self.view addSubview:_resultView];
    [_resultView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.bottom.mas_equalTo(self.view);
        make.top.mas_equalTo(_searchTextField.mas_bottom).offset(40);
    }];
}

- (UILabel *)errorLabel
{
    if (!_errorLabel){
        _errorLabel = [UILabel new];
        _errorLabel.textColor = [UIColor redColor];
        _errorLabel.fontSize = 15;
        _errorLabel.textAlignment = NSTextAlignmentCenter;
        [self.view addSubview:_errorLabel];
        [_errorLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.mas_equalTo(self.view.mas_centerX);
            make.top.mas_equalTo(_searchTextField.mas_bottom).offset(25);
        }];
    }
    return _errorLabel;
}

- (void)searchAction{
    [self.view endEditing:YES];
    
    NSString * oidString = _searchTextField.text;
    
    if (oidString.length == 0){
        [self.view showTip:@"请输入OID~"];
        return;
    }
    [BaseHttpTool get:kAPI_OID_SEARCH_OID_NEW params:@{@"type":@"2",@"url":oidString} success:^(id responseObj) {
        MFWJson * json = [MFWJson jsonWithObject:responseObj];
        BOOL isOID = [json booleanValueForKey:@"parameter"];
        if (!isOID){
            self.errorLabel.text = @"没有查询到OID信息，请重新输入查询！";
            _resultView.hidden = YES;
        }else
        {
            MFWJson * subJson = [json jsonForKey:@"rows"];
            _resultView.hidden =NO;
            [_resultView setWithJson:subJson];
        }
        
    } failure:^(NSError *error) {
        [self.view showTip:error.localizedDescription];
    }];
}

- (void)backAction{
    [self.navigationController popViewControllerAnimated:YES];
}


#pragma mark - UITextField delegate
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField
{
    self.errorLabel.text = @"";
    return YES;
}// return NO to disallow editing.


/*
 if （包含 http://）
 {
    if (包含 三个解析中心)
    {
        将oid=后面截取
        请求 http://www.e-oid.org.cn/oid/interfaceapp/search_oid?oid=&ip=&terminal=1&resolution=
    }else
    {
        请求  http://www.e-oid.org.cn/oid/interfaceapp/search_oid_url type = 1
    }
 }else
 {
    if (包含 oid:)
    {
        请求 http://www.e-oid.org.cn/oid/interfaceapp/search_oid_url type = 2
    }else
    {
        直接请求
    }
 }
 
 
 */




@end
